package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.data.RobotRoomMapper;
import io.xxx.xbutler.data.RoomContactMapper;
import io.xxx.xbutler.data.RoomMapper;
import io.xxx.xbutler.domain.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建群回调
 *
 * <pre>
 * {
 *     "nType": 4508,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcChatRoomId": "",
 *         "vcChatRoomSerialNo": "7EB5D10656A7AD7AD034C27B229B9899",
 *         "nUserCount": 3,
 *         "vcName": "",
 *         "vcBase64Name": "",
 *         "vcAdminWxId": "",
 *         "vcAdminSeriaNo": "",
 *         "vcHeadImg": "",
 *         "dtCreateTime": "2019-08-07T11:25:12.7012366+08:00",
 *         "vcMsgResult": "SUCCESS",
 *         "vcFriendWxIds": [
 *             ""
 *         ],
 *         "vcFriendSeriaNos": [
 *             ""
 *         ]
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4508Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Resource
    private RobotMapper robotMapper;

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private RobotRoomMapper robotRoomMapper;

    @Resource
    private RoomContactMapper roomContactMapper;

    @Override
    public String name() {
        return "创建群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4508;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getRobotSerialNo();
        Robot robot = robotMapper.selectById(robotId);

        String roomId = data.getString("vcChatRoomId");
        String roomSerialNo = data.getString("vcChatRoomSerialNo");

        LocalDateTime roomCreatedTime = LocalDateTime.ofInstant(data.getInstant("dtCreateTime"), ZoneId.systemDefault());
        Room room = new Room()
                .setId(roomId)
                .setSerialNo(roomSerialNo)
                .setName(data.getString("vcName"))
                .setBase64Name(data.getString("vcBase64Name"))
                .setMemberCount(data.getInteger("nUserCount"))
                .setAvatar(data.getString("vcHeadImg"))
                .setCreatedTime(roomCreatedTime)
                .setUpdatedTime(roomCreatedTime);

        RobotRoom robotRoom = new RobotRoom()
                .setId(idGenerator.next())
                .setRobotId(robotId)
                .setRobotSerialNo(robotSerialNo)
                .setRoomId(roomId)
                .setRoomSerialNo(roomSerialNo)
                .setJoinTime(roomCreatedTime)
                .setInviterId(robotId)
                .setInviterSerialNo(robotSerialNo)
                .setInviterName(robot.getName())
                .setInviterAvatar(robot.getAvatar())
                .setInvitationTime(roomCreatedTime)
                .setCreatedTime(now)
                .setUpdatedTime(now);

        JSONArray friendWxIds = data.getJSONArray("vcFriendWxIds");
        JSONArray friendSerialNos = data.getJSONArray("vcFriendSerialNos");
        List<RoomContact> roomContacts = new ArrayList<>(friendWxIds.size());
        for (int i = 0; i < friendWxIds.size(); i++) {
            String friendWxId = friendWxIds.getString(i);
            String friendSerialNo = friendSerialNos.getString(i);

            RoomContact roomContact = new RoomContact()
                    .setId(idGenerator.next())
                    .setRoomId(roomId)
                    .setRoomSerialNo(roomSerialNo)
                    .setContactId(friendWxId)
                    .setContactSerialNo(friendSerialNo)
                    .setJoinTime(roomCreatedTime)
                    .setInventorId(robotId)
                    .setInventorSerialNo(robotSerialNo)

                    .setCreatedTime(now)
                    .setUpdatedTime(now);
            roomContacts.add(roomContact);
        }

        transactionTemplate.executeWithoutResult(_ -> {
            roomMapper.insert(room);
            robotRoomMapper.insert(robotRoom);
            roomContactMapper.insert(roomContacts);
        });
    }
}
